package com.example.demo.cdesign.web;

import com.example.demo.cdesign.entity.*;
import com.example.demo.cdesign.service.*;
import com.example.demo.common.Pagination;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.rosuda.REngine.REXP;
import org.rosuda.REngine.REXPMismatchException;
import org.rosuda.REngine.REngineException;
import org.rosuda.REngine.Rserve.RserveException;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.util.Date;
import java.util.List;
import java.util.Map;

@RestController
public class HwrPowerForecastController {

    @Resource
    private HwrPowerForecastService hwrPowerForecastService;
    @Resource
    private ChannelService channelService;
    @Resource
    private ReactorService reactorService;
    @Resource
    private ReloadService reloadService;
    @Resource
    private CurrentDataService currentDataService;
    @Resource
    private JavaR javaR;

    @RequestMapping("JR_upd")
    public REXP JR_upd() {
        return javaR.runR_upd();
    }

    @RequestMapping("JR_rem")
    public List<String> JR_rem(String modelStart, String modelEnd, boolean ctnue, HttpServletRequest request) {
        request.getSession().setAttribute("modelStart", modelStart);
        request.getSession().setAttribute("modelEnd", modelEnd);
//        String taskId = DateFormatUtils.format(new Date(), "yyyyMMdd");
        String taskId = hwrPowerForecastService.generateTaskId();
        HwrPowerForecast query = new HwrPowerForecast();
        query.setTaskId(taskId);
        List<HwrPowerForecast> list = hwrPowerForecastService.find(query);
        for (HwrPowerForecast o : list) {
            hwrPowerForecastService.delete(o);
        }
        HwrPowerForecast hwrPowerForecast = new HwrPowerForecast();
        hwrPowerForecastService.save(hwrPowerForecast);
        try {
            javaR.deleteFiles(taskId, ctnue);
            javaR.runR_rem(modelStart, modelEnd, taskId);
            List<String> codes = javaR.getFile(taskId);
            hwrPowerForecast.setCodes(StringUtils.join(codes, ","));
            hwrPowerForecastService.save(hwrPowerForecast);
            return codes;
        } catch (Exception e) {
            e.printStackTrace();
            List<String> codes = javaR.getFile(taskId);
            hwrPowerForecast.setCodes(StringUtils.join(codes, ","));
            hwrPowerForecastService.save(hwrPowerForecast);
            return codes;
        }
    }

    @RequestMapping("JR_pre")
    public String[][] JR_pre(Double fpd, String channel, Double yewei_avg, String[] list1, String[] list2, String avgburnup, String chp, String taskID) throws REXPMismatchException, RserveException {
        return javaR.tDim(javaR.twoDim(javaR.runR_Pre(fpd, channel, yewei_avg, list1, list2, avgburnup, chp, taskID).asStrings(), 22));
    }

    @RequestMapping("JR_shm")
    public String[][] JR_shm() throws REXPMismatchException, RserveException {
        return javaR.tDim(javaR.twoDim(javaR.runR_shm().asStrings(), 22));
    }

    @RequestMapping("stop")
    public boolean stop() {
        try {
            return javaR.stop();
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    @RequestMapping("start")
    public boolean start() {
        try {
            return javaR.start();
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    @RequestMapping("channel_list")
    public Pagination channel_list(HttpServletRequest request, ChannelEntity channelEntity) {
        return channelService.pagination(request, channelEntity);
    }

    @RequestMapping("reactor_list")
    public Pagination reactor_list(HttpServletRequest request, ReactorEntity reactorEntity) {
        return reactorService.pagination(request, reactorEntity);
    }

    @RequestMapping("reload_list")
    public Pagination reload_list(HttpServletRequest request, ReloadEntity reloadEntity) {
        return reloadService.pagination(request, reloadEntity);
    }

    @RequestMapping("get")
    public HwrPowerForecast get(HwrPowerForecast hwrPowerForecast) {
        return hwrPowerForecastService.find(hwrPowerForecast).stream().findFirst().orElse(null);
    }

    @RequestMapping("currentData")
    public CurrentDataEntity currentData(CurrentDataEntity currentDataEntity) {
        return currentDataService.find(currentDataEntity).stream().findFirst().orElse(null);
    }

    @RequestMapping("channel_done")
    public List<String> channel_done(String mode) {
        String newTaskId = "";
        if ("rem".equals(mode)) {
//            newTaskId = DateFormatUtils.format(new Date(), "yyyyMMdd");
            newTaskId = hwrPowerForecastService.generateTaskId();
        } else {
            newTaskId = DateFormatUtils.format(new Date(), "yyyyMMdd");
            List<HwrPowerForecast> list = hwrPowerForecastService.find(new HwrPowerForecast());
            if (list.size() > 0) {
                newTaskId = list.get(0).getTaskId();
//
            }
        }
        return javaR.getFile(newTaskId);
    }

    @RequestMapping(value = "remHistory")
    public Pagination list(HttpServletRequest request, HwrPowerForecast hwrPowerForecast) {
        return hwrPowerForecastService.pagination(request, hwrPowerForecast);
    }

    @RequestMapping("updateFile")
    public void updateFile(File file) {
        System.out.println(file != null ? file.getName() : "");
    }

    @RequestMapping("test")
    public String[][] test(Double fpd, String channels, Double yewei_avg, String[] list1, String[] list2, String avgburnup, String chp, String taskID) {
        try {
            REXP rexp = javaR.runR_Pre(fpd, channels, yewei_avg, list1, list2, avgburnup, chp, taskID);
            return javaR.twoDim(rexp != null ? rexp.asStrings() : new String[]{}, 22);
        } catch (REXPMismatchException e) {
            e.printStackTrace();
            return null;
        }
    }

    //    @RequestMapping("show_data")
//    public String[][] show_data(){
//           try{
//               REXP rexp = javaR.runR_shd();
//               return javaR.tDim(javaR.twoDim(rexp != null ? rexp.asStrings() : new  String[]{}, 3));
//           }catch (REXPMismatchException e){
//               e.printStackTrace();
//               return null;
//           }
//    }
    @RequestMapping("show_data")
    public List<Map> show_data(String fpd_from, String fpd_end, String unitCode) throws REXPMismatchException {

        return javaR.runR_shd(fpd_from, fpd_end, unitCode);
    }

    @RequestMapping("rserveRun")
    public boolean rserveRun() {
        return javaR.rserveRun;
    }

    @RequestMapping("killP")
    public String killP() throws REXPMismatchException, RserveException {
        return javaR.killServe(javaR.rc);
    }

    @RequestMapping("shutDown")
    public String shutDown() throws REXPMismatchException, RserveException {
        return javaR.shutDown(javaR.rc);
    }

    @RequestMapping("getReactorList")
    public List<ReactorEntity> getFpdList(ReactorEntity reactorEntity) {
        List<ReactorEntity> listReactor = reactorService.find(reactorEntity);
        return listReactor;
    }


    @RequestMapping("index")
    public ModelAndView index(ModelAndView modelAndView) {
        modelAndView.setViewName("index");
        return modelAndView;
    }

    @RequestMapping("page")
    public ModelAndView page(ModelAndView modelAndView) throws REXPMismatchException, REngineException {
//        modelAndView.addObject("TestUtils",TestUtils.TestUtils());
//        modelAndView.addObject("javaR_upd", javaR.runR_upd());
//        modelAndView.addObject("javaR_rem", javaR.runR_rem(380));
//        modelAndView.addObject("javaR_pre", javaR.runR_Pre("C05"));
        List<HwrPowerForecast> list = hwrPowerForecastService.find(new HwrPowerForecast());
        modelAndView.addObject("list", list);

        modelAndView.setViewName("page");
        return modelAndView;
    }

    @RequestMapping("page1")
    public ModelAndView page1(ModelAndView modelAndView) {
        modelAndView.setViewName("page1");
        return modelAndView;
    }

    @RequestMapping("page2")
    public ModelAndView page2(ModelAndView modelAndView) {
        List<HwrPowerForecast> list = hwrPowerForecastService.find(new HwrPowerForecast());
        modelAndView.addObject("list", list);
        modelAndView.setViewName("page2");
        return modelAndView;
    }

    @RequestMapping("page3")
    public ModelAndView page3(ModelAndView modelAndView) {
        modelAndView.setViewName("page3");
        return modelAndView;
    }

    @RequestMapping("page4")
    public ModelAndView page4(ModelAndView modelAndView) {
        modelAndView.setViewName("page4");
        return modelAndView;
    }
}
